<?php
/**
 * Created by PhpStorm.
 * User: lzhy@lzhy.me
 * Date: 2019/2/28 0028
 * Time: 15:35
 */
namespace Api\Event;

class WechatJsPayEvent
{
    /**
     * 生成js_pay参数
     * @param $appId
     * @param $key
     * @param $prepayId
     * @return false|string
     */
    public static function buildParamsStr($appId,$key,$prepayId)
    {
        $data = [
            'appId'     => $appId,
            'timeStamp' => (string)time(),
            'nonceStr'  => ToolEvent::generatorRandStr(),
            'package'   => 'prepay_id='.$prepayId,
            'signType'  => 'MD5',
        ];
        $data['paySign'] = ToolEvent::weChatMd5Sign($data,$key);
        return json_encode($data);
    }

    /**
     * 生成js_pay页面
     * @param $params
     * @param $jumpUrl
     * @param $debug
     * @return string
     */
    public static function html($params,$jumpUrl = '',$debug = false)
    {
        $html = '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"/><meta name="viewport"content="width=device-width,initical-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/><title>微信安全支付</title><style type="text/css"media="screen">html,head,body,div,ul,li{margin:0;padding:0}img{vertical-align:middle}em,i{font-style:normal}li{list-style:none}a{text-decoration:none}body,button,input,select,textarea{font:100%/1"Microsoft YaHei","Helvetica Neue",Helvetica,STHeiTi}body{position:fixed;top:0;left:0;right:0;bottom:0;background-color:#eee}.ta{text-align:center}.red{color:red!important}.container>.pay_title>img{width:101%}.container>.pay_button>.back_go_pay{display:block;height:50px;line-height:50px;width:95%;margin:0 auto;background-color:#4cb131;color:#fff;letter-spacing:2px;font-size:18px;border-radius:8px;border:0;outline:0}.container>p{position:fixed;width:100%;color:#ababab;font-size:14px}.container>.company{bottom:40px}.container>.phone{bottom:15px}</style></head><body unselectable="on"onselectstart="return false;"id="body"><div class="container"><div class="pay_title ta"><img src="'.self::_img().'"alt=""></div><div class="pay_button"><button type="button"class="back_go_pay ta"onclick="onBridgeReady()">确定支付</button></div><p class="company ta">支付安全由中国人民财产保险股份有限公司承保</p><p class="ta phone">7&times;24小时热线：0755—86010333</p></div><script>window.onload=function(){document.getElementById(\'body\').addEventListener(\'touchmove\',function(){event.preventDefault()});function onBridgeReady(){WeixinJSBridge.invoke(\'getBrandWCPayRequest\','.$params.',function(res){if(res.err_msg===\'get_brand_wcpay_request:ok\'){location.href="'.$jumpUrl.'";}else{console.log(res.err_msg)}})}if(typeof WeixinJSBridge===\'undefined\'){if(document.addEventListener){document.addEventListener(\'WeixinJSBridgeReady\',onBridgeReady,false)}else if(document.attachEvent){document.attachEvent(\'WeixinJSBridgeReady\',onBridgeReady);document.attachEvent(\'onWeixinJSBridgeReady\',onBridgeReady)}}else{onBridgeReady()}};';
        if($debug){
            $html .= '(function(){var script=document.createElement(\'script\');script.src="//cdn.jsdelivr.net/npm/eruda";document.body.appendChild(script);script.onload=function(){eruda.init()}})();';
        }
        $html .= '</script></body></html>';

        return $html;
    }

    /**
     * base64图片
     * @return string
     */
    private static function _img()
    {
        return '';
    }
}